package com.woniuxy.controller;

import com.woniuxy.entity.DTO.ShopProductsDTO;
import com.woniuxy.entity.ShopProducts;
import com.woniuxy.service.ShopProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.utils.ResponseResult;

import java.util.List;


/**
 * (ShopProducts)表控制层
 *
 * @author makejava
 * @since 2025-07-10 11:54:56
 */
@RestController
@RequestMapping("shopProducts")
public class ShopProductsController {
    /**
     * 服务对象
     */
    @Autowired
    private ShopProductsService shopProductsService;
    @GetMapping("/get")
    public String get(){
        return "ResponseRes";
    }
    /** 查询所有商品*/
    @GetMapping("/all")
    public ResponseResult<List<ShopProducts>> findAll() {
        return ResponseResult.success(this.shopProductsService.findAll());
    }

    /**
     * 分页查询
     *
     * @param shopProducts 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseResult<Page<ShopProducts>> queryByPage(ShopProducts shopProducts, PageRequest pageRequest) {
        return ResponseResult.success(this.shopProductsService.queryByPage(shopProducts, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseResult<ShopProducts> queryById(@PathVariable("id") Long id) {
        return ResponseResult.success(this.shopProductsService.queryById(id));
    }
    /**
     * 查询热销商品数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/hot/{id}")
    public ResponseResult<List<ShopProducts>> queryHotSalesById(@PathVariable("id") Integer id) {
        return ResponseResult.success(this.shopProductsService.queryHotSalesById(id));
    }
    /** 秒杀商品*/
    @GetMapping("/kill/{id}")
    public ResponseResult<List<ShopProductsDTO>> queryKillSalesById(@PathVariable("id") Integer id) {
        List<ShopProductsDTO> dtos = this.shopProductsService.queryKillSalesById(id);
        for (ShopProductsDTO dto : dtos) {
            dto.setIskill(true);
        }
        return ResponseResult.success(dtos);
    }

    /**
     * 新增数据
     *
     * @param shopProducts 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseResult<ShopProducts> add(ShopProducts shopProducts) {
        return ResponseResult.success(this.shopProductsService.insert(shopProducts));
    }

    /**
     * 编辑数据
     *
     * @param shopProducts 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseResult<ShopProducts> edit(ShopProducts shopProducts) {
        return ResponseResult.success(this.shopProductsService.update(shopProducts));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseResult<Boolean> deleteById(Long id) {
        return ResponseResult.success(this.shopProductsService.deleteById(id));
    }

}

